/**
 * 日志控制器
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Controller, 
  Get, 
  Query, 
  Delete, 
  Param,
  UseGuards,
  HttpCode,
  HttpStatus,
  Res,
} from '@nestjs/common';
import { 
  ApiTags, 
  ApiOperation, 
  ApiResponse,
  ApiQuery,
} from '@nestjs/swagger';
import { Response } from 'express';
import { LogService } from './log.service';
import { LogExportService } from './log-export.service';
import { QueryLogDto } from './dto/query-log.dto';
import { Log } from './entities/log.entity';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermissions } from '../auth/decorators/permissions.decorator';
import { Log as LogDecorator } from '../../common/decorators/log.decorator';
import { LogType } from '../../interfaces/log.interface';
import { LogStatsDto } from './dto/log-stats.dto';

@ApiTags('日志管理')
@UseGuards(JwtAuthGuard)
@Controller('log')
export class LogController {
  constructor(
    private readonly logService: LogService,
    private readonly logExportService: LogExportService,
  ) {}

  @Get()
  @RequirePermissions('log:read')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志管理',
    action: '查询日志',
    description: '查询系统日志列表',
  })
  @ApiOperation({ summary: '查询日志列表' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '查询成功',
    type: [Log] 
  })
  async findAll(@Query() query: QueryLogDto) {
    const [logs, total] = await this.logService.findAll(query);
    return {
      items: logs,
      total,
      page: query.page,
      pageSize: query.pageSize,
    };
  }

  @Get('export')
  @RequirePermissions('log:export')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志管理',
    action: '导出日志',
    description: '导出系统日志',
  })
  @ApiOperation({ summary: '导出日志' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '导出成功' 
  })
  async export(@Query() query: QueryLogDto, @Res() response: Response) {
    return this.logExportService.exportToExcel(query, response);
  }

  @Delete('clean/:days')
  @HttpCode(HttpStatus.NO_CONTENT)
  @RequirePermissions('log:clean')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志管理',
    action: '清理日志',
    description: '清理系统日志',
  })
  @ApiOperation({ summary: '清理日志' })
  @ApiQuery({ 
    name: 'days', 
    description: '保留天数', 
    type: Number,
    example: 30 
  })
  @ApiResponse({ 
    status: HttpStatus.NO_CONTENT, 
    description: '清理成功' 
  })
  async clean(@Param('days') days: string) {
    await this.logService.clean(+days);
  }

  @Get('stats')
  @RequirePermissions('log:read')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志管理',
    action: '统计日志',
    description: '获取日志统计信息',
  })
  @ApiOperation({ summary: '获取日志统计信息' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '获取统计信息成功',
    type: LogStatsDto,
  })
  async getStats() {
    return this.logService.getStats();
  }

  @Get('trend')
  @RequirePermissions('log:read')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志管理',
    action: '日志趋势',
    description: '获取日志趋势数据',
  })
  @ApiOperation({ summary: '获取日志趋势' })
  @ApiQuery({
    name: 'days',
    description: '统计天数',
    required: false,
    type: Number,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '获取趋势数据成功',
    type: [Object],
  })
  async getTrend(@Query('days') days: string) {
    return this.logService.getTrend(days ? parseInt(days) : 7);
  }
} 